package com.spark.lola.controller.purchaser;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spark.lola.entity.sup.BankInfo;
import com.spark.lola.mapper.sup.BankInfoMapper;
import com.spark.lola.request.BankInfoRequest;
import com.spark.lola.service.sup.IBankInfoService;
import com.spark.lola.support.api.entity.EnterpriseTokenUser;
import com.spark.lola.support.common.constant.BooleanConstant;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.common.util.DataBeanUtils;
import com.spark.lola.support.common.util.ResponseBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收款方式
 *
 * @Author: DengYeJun
 * @Date: 2020/5/22 14:26
 */
@RestController
@RequestMapping("/api/pur/bank/")
@Api(value = "/api/pur/bank/", tags = "供应商收款管理")
public class BankController {
    @Autowired
    private BankInfoMapper bankInfoMapper;
    @Autowired
    private IBankInfoService bankInfoService;

    @ApiOperation(value = "查询收款列表")
    @ApiImplicitParam(name = "supplierId", value = "供应商id", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/list/{supplierId}", method = RequestMethod.POST)
    public ResponseBean index(@PathVariable long supplierId) {
        QueryWrapper<BankInfo> query = new QueryWrapper<>();
        query.eq("supplier_id", supplierId);
        query.eq("used", CommonConstant.Y);
        query.orderByDesc("auto");
        List<BankInfo> lst = bankInfoMapper.selectList(query);
        return ResponseBean.success(lst);
    }

    @ApiOperation(value = "查询收款详情")
    @RequestMapping(value = "/findInfo/{id}", method = RequestMethod.POST)
    public ResponseBean findInfo(@PathVariable Long id) {
        BankInfo model = new BankInfo();
        if (id != 0) {
            model = bankInfoService.getById(id);
        }
        return ResponseBean.success(model);
    }

    @ApiOperation(value = "保存/更新收款信息")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean save(@RequestBody BankInfoRequest form, @SessionAttribute EnterpriseTokenUser<?> tokenUser) {
        BankInfo model = new BankInfo();
        if (form.getId() != null) {
            // 禁止修改项 做空处理
            model = bankInfoService.getById(form.getId());
        }
        BeanUtils.copyProperties(form, model);
        model.setCreator(tokenUser.getShiroUser().getRealName());
        model.setCreator(tokenUser.getShiroUser().getRealName());
        bankInfoService.saveOrUpdate(model);
        if (model.getAuto().equals(BooleanConstant.TRUE)) {
            // 默认收款方式 重置其他收款信息
            bankInfoMapper.resetAuto(model.getSupplierId());
        }
        bankInfoService.saveOrUpdate(model);
        return ResponseBean.success(model);
    }

    @ApiOperation(value = "指定默认收款信息")
    @RequestMapping(value = "/auto", method = RequestMethod.POST)
    public ResponseBean auto(@RequestBody JSONObject jsonObject) {
        Long id = jsonObject.getLong("id");
        Long supplierId = jsonObject.getLong("supplierId");
        if (id == null || supplierId == null) {
            return ResponseBean.error("参数为空");
        }
        bankInfoMapper.resetAuto(supplierId);
        bankInfoMapper.setAuto(id);
        return ResponseBean.success();
    }

//    @ApiOperation(value = "删除收款信息")
//    @RequestMapping(value = "/del/{id}", method = RequestMethod.POST)
//    public ResponseBean del(@PathVariable Long id) {
//        bankInfoService.removeById(id);
//        return ResponseBean.success();
//    }

}
